package com.yequan.leetcode.array.trap_42;

/**
 * @author : Administrator
 * @date : 2020/4/12
 */
public class RecordTrap {


    public int trap1(int[] height) {
        if (null == height || height.length < 3) {
            return 0;
        }
        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, res = 0;
        while (left <= right) {
            maxLeft = Math.max(maxLeft, height[left]);
            maxRight = Math.max(maxRight, height[right]);
            if (maxLeft <= maxRight) {
                res += maxLeft - height[left++];
            } else {
                res += maxRight - height[right--];
            }
        }
        return res;
    }

    public int trap2(int[] height) {
        if (null == height || height.length < 3) {
            return 0;
        }
        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, res = 0;
        while (left <= right) {
            maxLeft = Math.max(maxLeft, height[left]);
            maxRight = Math.max(maxRight, height[right]);
            if (maxLeft <= maxRight) {
                res += maxLeft - height[left++];
            } else {
                res += maxRight - height[right--];
            }
        }
        return res;
    }

    public int trap3(int[] height) {
        if (null == height || height.length < 3) {
            return 0;
        }
        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, res = 0;
        while (left <= right) {
            maxLeft = Math.max(maxLeft, height[left]);
            maxRight = Math.max(maxRight, height[right]);
            if (maxLeft <= maxRight) {
                res += maxLeft - height[left++];
            } else {
                res += maxRight - height[right--];
            }
        }
        return res;
    }

    public int trap4(int[] height) {
        if (null == height || height.length < 3) {
            return 0;
        }
        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, ans = 0;
        while (left <= right) {
            maxLeft = Math.max(maxLeft, height[left]);
            maxRight = Math.max(maxRight, height[right]);
            if (maxLeft <= maxRight) {
                ans += maxLeft - height[left++];
            } else {
                ans += maxRight - height[right--];
            }
        }
        return ans;
    }

    public int trap5(int[] height) {
        if (null == height || height.length < 3) {
            return 0;
        }
        int left = 0, right = height.length - 1, maxLeft = 0, maxRight = 0, ans = 0;
        while (left <= right) {
            maxLeft = Math.max(maxLeft, height[left]);
            maxRight = Math.max(maxRight, height[right]);
            if (maxLeft <= maxRight) {
                ans += maxLeft - height[left++];
            } else {
                ans += maxRight - height[right--];
            }
        }
        return ans;
    }

}
